<!DOCTYPE html>
<html>
<head>
    <title>用户管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <div class="container">
        <h1 class="page-title">用户管理</h1>
        <div class="nav-menu">
            <a href="{{ url_for('index') }}" class="nav-item">返回首页</a>
            <a href="#" class="nav-item add-btn" onclick="showModal()">新增用户</a>
        </div>
        
        <!-- 新增用户模态框 -->
        <div id="addModal" class="modal">
            <div class="modal-content">
                <span class="close" onclick="hideModal()">&times;</span>
                <h2>新增用户</h2>
                <form action="{{ url_for('add_user') }}" method="POST">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" required>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" name="password" required>
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" id="name" name="name" required>
                    </div>
                    <div class="form-group">
                        <label for="role">角色</label>
                        <select id="role" name="role" required>
                            <option value="staff">普通员工</option>
                            <option value="admin">管理员</option>
                        </select>
                    </div>
                    <button type="submit" class="nav-item add-btn">保存</button>
                </form>
            </div>
        </div>
        
        <!-- 编辑用户模态框 -->
        <div id="editModal" class="modal">
            <div class="modal-content">
                <span class="close" onclick="hideEditModal()">&times;</span>
                <h2>编辑用户</h2>
                <form id="editForm">
                    <input type="hidden" id="edit_id" name="id">
                    <div class="form-group">
                        <label for="edit_username">用户名</label>
                        <input type="text" id="edit_username" name="username" required>
                    </div>
                    <div class="form-group">
                        <label for="edit_password">密码</label>
                        <input type="password" id="edit_password" name="password" placeholder="不修改请留空">
                    </div>
                    <div class="form-group">
                        <label for="edit_name">姓名</label>
                        <input type="text" id="edit_name" name="name" required>
                    </div>
                    <div class="form-group">
                        <label for="edit_mobile">手机号</label>
                        <input type="text" id="edit_mobile" name="mobile">
                    </div>
                    <div class="form-group">
                        <label for="edit_role">角色</label>
                        <select id="edit_role" name="role" required>
                            <option value="staff">普通员工</option>
                            <option value="admin">管理员</option>
                        </select>
                    </div>
                    <button type="button" onclick="submitEdit()" class="nav-item add-btn">保存</button>
                </form>
            </div>
        </div>
        
        <div class="section">
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>姓名</th>
                        <th>手机号</th>
                        <th>角色</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr>
                        <td>{{ user.id }}</td>
                        <td>{{ user.username }}</td>
                        <td>{{ user.name }}</td>
                        <td>{{ user.mobile or '' }}</td>
                        <td>{{ '管理员' if user.role == 'admin' else '普通员工' }}</td>
                        <td>{{ '启用' if user.active else '禁用' }}</td>
                        <td>
                            <a href="#" class="action-btn edit-btn" onclick="showEditModal({{ user.id }})">编辑</a>
                            <a href="#" class="action-btn delete-btn">{{ '禁用' if user.active else '启用' }}</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <script>
        function showModal() {
            document.getElementById('addModal').style.display = 'block';
        }
        
        function hideModal() {
            document.getElementById('addModal').style.display = 'none';
        }
        
        function showEditModal(userId) {
            // 获取用户信息
            fetch(`/users/${userId}`)
                .then(response => response.json())
                .then(data => {
                    if (data.code === 0) {
                        const user = data.data;
                        document.getElementById('edit_id').value = user.id;
                        document.getElementById('edit_username').value = user.username;
                        document.getElementById('edit_name').value = user.name;
                        document.getElementById('edit_mobile').value = user.mobile || '';
                        document.getElementById('edit_role').value = user.role;
                        document.getElementById('edit_password').value = '';
                        document.getElementById('editModal').style.display = 'block';
                    } else {
                        alert('获取用户信息失败：' + data.message);
                    }
                })
                .catch(error => {
                    alert('获取用户信息失败：' + error);
                });
        }

        function hideEditModal() {
            document.getElementById('editModal').style.display = 'none';
        }

        function submitEdit() {
            const userId = document.getElementById('edit_id').value;
            const formData = {
                username: document.getElementById('edit_username').value,
                name: document.getElementById('edit_name').value,
                mobile: document.getElementById('edit_mobile').value,
                role: document.getElementById('edit_role').value,
                password: document.getElementById('edit_password').value
            };

            fetch(`/users/${userId}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    alert('更新成功');
                    hideEditModal();
                    location.reload(); // 刷新页面显示更新后的数据
                } else {
                    alert('更新失败：' + data.message);
                }
            })
            .catch(error => {
                alert('更新失败：' + error);
            });
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            var addModal = document.getElementById('addModal');
            var editModal = document.getElementById('editModal');
            if (event.target == addModal) {
                addModal.style.display = "none";
            }
            if (event.target == editModal) {
                editModal.style.display = "none";
            }
        }
    </script>

    <style>
    .modal {
        display: none;
        position: fixed;
        z-index: 1;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.4);
    }

    .modal-content {
        background-color: #fefefe;
        margin: 15% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
        max-width: 500px;
    }

    .close {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
        cursor: pointer;
    }

    .close:hover {
        color: black;
    }

    .form-group {
        margin-bottom: 15px;
    }

    .form-group label {
        display: block;
        margin-bottom: 5px;
    }

    .form-group input,
    .form-group select {
        width: 100%;
        padding: 8px;
        border: 1px solid #ddd;
        border-radius: 4px;
    }
    </style>
</body>
</html> 